#include "newprojectwizard.h"
#include "ui_newprojectwizard.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QXmlStreamWriter>
#include <QWizard>

NewProjectWizard::NewProjectWizard(QWidget *parent) :
    QWizard(parent),
    m_ui(new Ui::NewProjectWizard)
{
    m_ui->setupUi(this);
}

NewProjectWizard::~NewProjectWizard()
{
    delete m_ui;
}

void NewProjectWizard::changeEvent(QEvent *e)
{
    QWizard::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void NewProjectWizard::on_NewProjectWizard_customButtonClicked(int which)
{
    if(which == QWizard::FinishButton)
    {
        qDebug("Generating xml");
        QString projectFile = QFileDialog::getSaveFileName(this, "New project file","","Project  file(*.xml)");
        QFile file(projectFile);
        if(!file.open(QIODevice::WriteOnly))
        {
            QMessageBox::critical(0, "Can't create project", file.errorString());
            return;
        }
        QXmlStreamWriter stream(&file);
        stream.setAutoFormatting(true);
        stream.writeStartDocument();
        stream.writeStartElement("Shushi");
        stream.writeAttribute("version","0.1 alpha");
        //project
        stream.writeStartElement("project");
        stream.writeTextElement("id", "123");
        stream.writeTextElement("name", m_ui->projectNameEdit->text());
        stream.writeTextElement("version", "1.0.1");
        stream.writeTextElement("author", "Edward");
        stream.writeTextElement("language", "cpp");
        stream.writeEndElement();
        //project
        //code
        stream.writeStartElement("code");
        stream.writeEndElement();
        //code

        stream.writeEndElement(); // bookmark
        stream.writeEndDocument();
        file.close();
    }
}
